<?php
//导航栏Xml
class Navxml{
	public function getNavXml($isAdmin=false){
		$xmlfile = APP_NAME.'/Lib/Xml/navigation.xml';
		$xmlparser = xml_parser_create();
		$fp = fopen($xmlfile, 'r');
		$xmldata = fread($fp, filesize($xmlfile));		
		xml_parse_into_struct($xmlparser,$xmldata,$values);
		xml_parser_free($xmlparser);
		if(!empty($values)){
			$nav = array();
			$allow=true;
			foreach($values as $k=>$v){
				if($k==0)continue;
				if($v['attributes']['TYPE']=="hidden" && !$this->isAdmin)$allow=false;
				if($v['type']=="open" && $allow){
					$key = explode("/",$v['attributes']['URL']);//提取最后的编号为索引
					$key = end(array_filter($key));
					if(!empty($key)){
						$nav[$key]['key'] = $key;
						$nav[$key]['title'] = $v['attributes']['TITLE'];
						$nav[$key]['url']  = $v['attributes']['URL'];
						$nav[$key]['name'] = $v['attributes']['NAME'];
						$nav[$key]['type'] = $v['attributes']['TYPE'];
					}
				}elseif($v['tag']=="ITEM"){
					if(!empty($key) && ($v['attributes']['TYPE']!="hidden" or $isAdmin)){
						$kkey = $v['attributes']['NAME'];
						$v['attributes']['value'] = $v['value'];
						$nav[$key]['item'][$kkey] = array_change_key_case($v['attributes']);
					}
				}elseif($v['type']=="close"){
					$key="";
					$allow=true;
				}
			}
			return $nav;
		}
		return false;
	}
	
	public function checkNav(){
		$arr = array();
		$admim = session(getOnlyName("admin"));//获取配置
		if($admim['user']['groups']==1){//超级管理员		
			$arr = $nav = $this->getNavXml();		
		}elseif($admim['user']['groups']==2){//普通管理员
			$power = unserialize($admim['user']['power']);
			$nav = $this->getNavXml();
			foreach($power as $k=>$v){//遍历数组
				
				//提取键值作比较
				$powerKey = array_flip(array_keys($v['item']));
				$navKey = array_flip(array_keys($nav[$k]['item']));
				$key = array_diff_key($navKey,$powerKey);//差值

				if(!empty($key)){
					foreach($key as $kk=>$vv){
						unset($nav[$k]['item'][$kk]);
					}				
				}
				$arr[$k] = $nav[$k];
			}
		}
		return $arr;
	}
}
